

var opn = require('opn')//这个用来打开网站、文件、可执行文件之类的
var express = require('express')
var config = require("./webpack.config-dev.js");
var proxyMiddleware = require('http-proxy-middleware')
var webpack = require('webpack');
var app = express()//nodu服务器
var compiler = webpack(config);//根据配置文件获取webpack处理对象


var proConfig = require('./config.js');
var port = proConfig.dev.port

var context = proConfig.dev.context
var proxypath = proConfig.dev.proxypath

var options = {
    target: proxypath,
    changeOrigin: true,
    secure: false
}
if (context.length) {
    app.use(proxyMiddleware(context, options))
}


var devMiddleware = require('webpack-dev-middleware')(compiler, {
  publicPath: config.output.publicPath,
  quiet: true
})
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
  log: () => {}
})

compiler.plugin('compilation', function (compilation) {
  compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
    hotMiddleware.publish({ action: 'reload' })
    cb()
  })
})

app.use(devMiddleware)
app.use(hotMiddleware)

var uri = 'http://b.zmlearn.com:' + port

module.exports = app.listen(port, function (err) {
 opn(uri)
})
